package com.acme.bankapp.service.bank;

import java.util.*;

import com.acme.bankapp.domain.bank.*;

/**
 * Class provides bank report functionality
 * @author alex
 */
public class BankReport {
	private Bank bank;
	
	/**
	 * @param bank the bank
	 */
	public BankReport(Bank bank) {
		this.bank = bank;
	}
	
	/**
	 * Get clients list
	 */
	public void getClientsList() {
		for (Client client : bank.getClients()) {
			System.out.println(client);
		}
	}
	
	/**
	 * Get accounts list
	 */
	public void getAccountsList() {
		for (Client client : bank.getClients()) {
			System.out.println(client);
			for (Account account : client.getAccounts().values()) {
				System.out.println(account);
			}
		}
	}
	
	/**
	 * Get number of clients
	 */
	public void getNumberOfClients() {
		System.out.println("=Bank clients count=");
		System.out.println(bank.getClientsCount());
	}
	
	/**
	 * Get number of accounts
	 */
	public void getNumberOfAccounts() {
		int accountsCount = 0;
		for (Client client : bank.getClients()) {
			accountsCount += client.getAccountsCount();
		}
		System.out.println("=Bank accounts count=");
		System.out.println(accountsCount);
	}
	
	/**
	 * Get accounts sorted by balance
	 */
	public void getAccountsSortedByBalance() {
		List<Account> sortedAccounts = new ArrayList<Account>();
		for (Client client : bank.getClients()) {
			for (Account account : client.getAccounts().values())
				sortedAccounts.add(account);
		}
		Collections.sort(sortedAccounts);
		
		System.out.println("=Bank accounts sorted by balance=");
		for (Account account : sortedAccounts) {
			System.out.println(account);
		}
	}
	
	/**
	 * Get bank credit
	 */
	public void getBankCredit() {
		int credit = 0;
		for (Client client : bank.getClients()) {
			for (Account account : client.getAccounts().values()) {
				if (account.getBalance() < 0)
					credit += account.getBalance();
			}
		}
		System.out.println("=Bank total credit=");
		System.out.println(credit);
	}
	
	/**
	 * @return the client account map
	 */
	public Map<Client, Map<Integer, Account>> getClientAccount() {
		Map<Client, Map<Integer, Account>> clientAccount = new HashMap<>();
		for (Client client : bank.getClients()) {
			clientAccount.put(client, client.getAccounts());
		}
		return clientAccount;
	}
}
